package account

import SecureController 
import finance.UserCashAccount 
import finance.UserCdAccount 

class HomeController extends SecureController {
    def index = { }
    
    def list_accounts = {
		def cash_accounts = UserCashAccount.findAll('from UserCashAccount uca where uca.user = ?',  [session.user])
		def cd_accounts = UserCdAccount.findAll('from UserCdAccount ucda where ucda.user = ?', [session.user])
		[user_cash_accounts: cash_accounts, user_cd_accounts: cd_accounts]
    }
    
    def add_account = {
    }
    
    def save_new_cash_account = {
		def userCashAccountInstance = new UserCashAccount(params)
		userCashAccountInstance.user = session.user
		userCashAccountInstance.currency = finance.Currency.get(1)
        userCashAccountInstance.save(flush: true)
    	redirect(controller: 'home', action:'list_accounts')
    }
    
    def save_new_cd_account = {
    	def userCdAccountInstance = new UserCdAccount(params)
    	userCdAccountInstance.start_date = new Date()
    	userCdAccountInstance.user = session.user
    	userCdAccountInstance.currency = finance.Currency.get(1)
        userCdAccountInstance.save(flush: true)
    	redirect(controller: 'home', action:'list_accounts')
    }
}